<?php

namespace iBrand\Shop\Core\Models;

use Illuminate\Database\Eloquent\Model;
use DB;

class CashRecord extends Model
{
    protected $guarded = ['id'];

    protected $appends = ['cash_type', 'cash_category'];

    const CATEGORY_DAILY_COST = 'daily_cost'; //日常支出
    const CATEGORY_REJECT_COST = 'reject_cost'; //退货支出
    const CATEGORY_OTHER_COST = 'other_cost';//其他支出
    const CATEGORY_REMAINING = 'remaining';//找零支出
    const CATEGORY_ORDER = 'order';//销售收入
    const CATEGORY_TRANSFER = 'transfer';//调货收入
    const CATEGORY_OTHER = 'other';//其他收入

    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);

        $prefix = config('ibrand.app.database.prefix', 'ibrand_');

        $this->setTable($prefix . 'o2o_cash_records');
    }

    /*public function setAmountAttribute($value)
    {
        $this->attributes['amount'] = $value * 100;
    }

    public function getAmountAttribute()
    {
        return $this->attributes['amount'] / 100;
    }*/

    public function getCashTypeAttribute()
    {
        if ($this->amount > 0) {
            return '收入';
        }

        return '支出';
    }

    public function getCashCategoryAttribute()
    {
        switch ($this->category) {
            case 'daily_cost':
                return '日常支出';
                break;
            case 'reject_cost':
                return '退货支出';
                break;
            case 'remaining':
                return '销售找零';
                break;
            case 'other_cost':
                return '其他支出';
                break;
            case 'order':
                return '销售收入';
                break;
            case 'transfer':
                return '调货收入';
                break;
            default:
                return '其他收入';
        }
    }
}

